﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BasePlatformNet
{
	class LEDState
	{
		// 状态 byte[] 长度
		public static int stateLength = 16;

		// 状态 byte[]
		public byte[] stateBytes;

		// 状态有效标记
		public bool isValid = false;

		// 状态值
		public const int ON = 0;					// 开启
		public const int OFF = 1;					// 关闭

		public const int REAL = 1;					// 实效果
		public const int VIRTUAL = 0;				// 虚拟效果

		// 状态项
		public int STAT_Led_Power = ON;				// LED屏电源
		public int STAT_Fidelity = OFF;				// 低亮度保真功能
		public int STAT_Disp_Lock = OFF;			// 锁屏开关
		public int STAT_Disp_Black = OFF;			// 黑屏开关
		public int STAT_Point_Correction = OFF;		// 逐点校正
		public int STAT_Chroma_Adjust = OFF;		// 箱体色度调整
		public int STAT_Error_Check = OFF;			// 错误校验		默认关闭
		public int STAT_Virtual_Screen = REAL;		// 四灯虚拟屏

		public int STAT_Forced_Sync = ON;			// 强制同步		默认开启

		public int STAT_Contrast;					// 对比度		0~255

		public int STAT_Light_ALL;					// 整屏亮度	0~255
		public int STAT_Light_R;					// 红色亮度	0~255
		public int STAT_Light_G;					// 绿色亮度	0~255
		public int STAT_Light_B;					// 蓝色亮度	0~255

		public int STAT_Offset_Line = 0;			// 屏体相对偏移列数：像素为单位
		public int STAT_Offset_Row = 0;				// 屏体相对偏移行数：像素为单位

		// 构造函数
		public LEDState()
		{
			stateBytes = new byte[stateLength];
		}

		// 设置显示状态字节
		public void setStateBytes(byte[] value, int length)
		{
			Array.Copy(value, stateBytes, stateLength);
			isValid = true;
		}

		// 解析显示状态
		public void parseState()
		{
			if (!isValid)
				return;

			// 字节0
			if ((stateBytes[0] >> 0 & 0x1) == 0x0)		// bit0 - STAT_Led_Power - LED屏电源
				STAT_Led_Power = ON;
			else if ((stateBytes[0] >> 0 & 0x1) == 0x1)
				STAT_Led_Power = OFF;

			if ((stateBytes[0] >> 1 & 0x1) == 0x0)		// bit1 - STAT_Fidelity - 低亮度保真功能
				STAT_Fidelity = OFF;
			else if ((stateBytes[0] >> 1 & 0x1) == 0x1)
				STAT_Fidelity = ON;

			if ((stateBytes[0] >> 2 & 0x1) == 0x0)		// bit2 - STAT_Disp_Lock - 锁屏开关
				STAT_Disp_Lock = OFF;
			else if ((stateBytes[0] >> 2 & 0x1) == 0x1)
				STAT_Disp_Lock = ON;

			if ((stateBytes[0] >> 3 & 0x1) == 0x0)		// bit3 - STAT_Disp_Black - 锁屏开关
				STAT_Disp_Black = OFF;
			else if ((stateBytes[0] >> 3 & 0x1) == 0x1)
				STAT_Disp_Black = ON;

			if ((stateBytes[0] >> 4 & 0x1) == 0x0)		// bit4 - STAT_Disp_Black - 逐点校正
				STAT_Point_Correction = OFF;
			else if ((stateBytes[0] >> 4 & 0x1) == 0x1)
				STAT_Point_Correction = ON;

			if ((stateBytes[0] >> 5 & 0x1) == 0x0)		// bit5 - STAT_Chroma_Adjust - 箱体色度调整
				STAT_Chroma_Adjust = OFF;
			else if ((stateBytes[0] >> 5 & 0x1) == 0x1)
				STAT_Chroma_Adjust = ON;

			if ((stateBytes[0] >> 6 & 0x1) == 0x0)		// bit6 - STAT_Error_Check - 错误校验
				STAT_Error_Check = ON;
			else if ((stateBytes[0] >> 6 & 0x1) == 0x1)
				STAT_Error_Check = OFF;

			if ((stateBytes[0] >> 7 & 0x1) == 0x0)		// bit7 - STAT_Virtual_Screen - 四灯虚拟屏
				STAT_Virtual_Screen = REAL;
			else if ((stateBytes[0] >> 7 & 0x1) == 0x1)
				STAT_Virtual_Screen = VIRTUAL;

			// 字节1
			if ((stateBytes[1] >> 0 & 0x1) == 0x0)		// bit0 - STAT_Forced_Sync - 强制同步
				STAT_Forced_Sync = ON;
			else if ((stateBytes[1] >> 0 & 0x1) == 0x1)
				STAT_Forced_Sync = OFF;

			// 字节2 - 保留

			// 字节3
			STAT_Contrast = (stateBytes[3]);

			// 字节4
			STAT_Light_ALL = (stateBytes[4]);

			// 字节5
			STAT_Light_R = (stateBytes[5]);

			// 字节6
			STAT_Light_G = (stateBytes[6]);

			// 字节7
			STAT_Light_B = (stateBytes[7]);

			// 字节8、9
			STAT_Offset_Line = stateBytes[8] & 0xFF;
			STAT_Offset_Line |= ((stateBytes[9] << 8) & 0xFF00);

			// 字节10、11
			STAT_Offset_Row = stateBytes[10] & 0xFF;
			STAT_Offset_Row |= ((stateBytes[11] << 8) & 0xFF00);

			// 字节12~15 - 保留
		}

		// 获取显示状态字符串
		public string getStateStr()
		{
			return Tools.Byte2HexString(stateBytes);
		}

		// 获取显示状态字符串 - 友好模式
		public string getFriendlyStateStr()
		{
			string str = String.Empty;

			str += "STAT_Led_Power: " + getFriendlyStateValue(STAT_Led_Power) + "\t\n";
			str += "STAT_Fidelity: " + getFriendlyStateValue(STAT_Fidelity) + "\t\n";
			str += "STAT_Disp_Lock: " + getFriendlyStateValue(STAT_Disp_Lock) + "\t\n";
			str += "STAT_Disp_Black: " + getFriendlyStateValue(STAT_Disp_Black) + "\t\n";
			str += "STAT_Point_Correction: " + getFriendlyStateValue(STAT_Point_Correction) + "\t\n";
			str += "STAT_Chroma_Adjust: " + getFriendlyStateValue(STAT_Chroma_Adjust) + "\t\n";
			str += "STAT_Error_Check: " + getFriendlyStateValue(STAT_Error_Check) + "\t\n";
			str += "STAT_Virtual_Screen: " + getFriendlyStateValue(STAT_Virtual_Screen) + "\t\n";

			str += "STAT_Forced_Sync: " + getFriendlyStateValue(STAT_Forced_Sync) + "\t\n";
			str += "STAT_Contrast: " + STAT_Contrast + "\t\n";
			str += "STAT_Light_ALL: " + STAT_Light_ALL + "\t\n";
			str += "STAT_Light_R: " + STAT_Light_R + "\t\n";
			str += "STAT_Light_G: " + STAT_Light_G + "\t\n";
			str += "STAT_Light_B: " + STAT_Light_B + "\t\n";
			str += "STAT_Offset_Line: " + STAT_Offset_Line + "\t\n";
			str += "STAT_Offset_Row: " + STAT_Offset_Row + "\t\n";

			return str;
		}

		private string getFriendlyStateValue(int value)
		{
			if (value == ON)
			{
				return "ON";
			}
			else if (value == OFF)
			{
				return "OFF";
			}

			return "Unknown";
		}
	}
}
